import constant from '@/constant'
import {mutationPacking} from '../interceptor'

const {LOGIN, LOGOUT, UPDATE_PSW, WORK_LIST, SUBBORDINATES, DELETE, TOGGLE, RESETPWD, ALL_LIST, UPDATE, INSERT, BASE_INFO} = constant.user

const mutations = {
    [LOGIN]: function (state, payload) {
        state.info.id = payload.id
        state.info.name = payload.name
        state.info.authenticated = true
        payload.token && (state.info.token = payload.token)
        // state.info.role = payload.roles && payload.roles[0]
        payload.role && (state.info.role = payload.role)
    },
    [LOGOUT]: function (state) {
        state.info.id = 0
        state.info.name = null
        state.info.token = null
        state.info.role = null
        state.info.authenticated = false
    },
    [UPDATE_PSW]: () => { },
    [WORK_LIST]: function (state, payload) {
        state.workList.data = payload
    },
    [SUBBORDINATES]: function (state, payload) {
        state.subordinates = {...payload, async_state: state.subordinates.async_state}
    },
    [ALL_LIST]: function (state, payload) {
        state.allList.data = payload.data
        state.allList.pagination = payload.meta.pagination
    },
    [UPDATE]: () => { },
    [DELETE]: () => { },
    [TOGGLE]: () => { },
    [INSERT]: () => { },
    [RESETPWD]: () => { },

    [BASE_INFO]: function (state, payload) {
        state.base.data = {
            name: payload.name || '',
            longitude: payload.longitude,
            latitude: payload.latitude
        }
    }
}

mutations[LOGIN].prototype.bindAttr = 'info.async_state'
mutations[UPDATE_PSW].prototype.bindAttr = 'update_async_state'
mutations[WORK_LIST].prototype.bindAttr = 'workList.async_state'
mutations[SUBBORDINATES].prototype.bindAttr = 'subordinates.async_state'
mutations[ALL_LIST].prototype.bindAttr = 'allList.async_state'
mutations[TOGGLE].prototype.bindAttr = 'toggle_async_state'
mutations[RESETPWD].prototype.bindAttr = 'reset_pwd_async_state'
mutations[UPDATE].prototype.bindAttr = 'update_async_state'
mutations[INSERT].prototype.bindAttr = 'insert_async_state'
mutations[DELETE].prototype.bindAttr = 'delete_async_state'
mutations[BASE_INFO].prototype.bindAttr = 'base.async_state'

export default mutationPacking(mutations)
